<?php


namespace app\admin\controller\System;


use app\admin\controller\BaseController;
use app\admin\model\System\SysPayModel;
use app\Request;
use app\Utils\ImageUtil;
use app\Utils\Util;

class SysPay extends BaseController
{
    /**
     * 列表
     * @param Request $request
     * @return array
     */
    public function getPayList(Request $request)
    {
        $where = [];
        $param = $request->param();
        $page = !empty($param['page']) ? $param['page'] : 1;
        $limit = !empty($param['limit']) ? $param['limit'] : 10;

        $list = SysPayModel::getSysPayListWhere($page,$limit,$where);
        $count = SysPayModel::getSysPayCountWhere($where);
        foreach ($list as &$item){
            $item['icon'] = ImageUtil::i()->replaceUrl($item['icon']);
            $extend_json = json_decode($item['extend_json'],true);
            if(!empty($extend_json)){
                foreach ($extend_json as $key=>$val){
                    $item[$key] = $val;
                }
            }
        }
        unset($item);
        return Util::returnPageJson(self::CODE_DATA,$count,$list,"");
    }

    /**
     * 信息修改
     * @param Request $request
     * @return array
     */
    public function editPayInfo(Request $request)
    {
        $param = $request->param();

        $data = [
            'name'=>!empty($param['name']) ? $param['name'] : "",
            //'type'=>!empty($param['type']) ? $param['type'] : 0,
            'sort'=>!empty($param['sort']) ? $param['sort'] : 0,
            'up_time'=>date("Y-m-d H:i:s",time()),
        ];
        $extend_json = [];
        if(!empty($param['app_id'])){
            $extend_json['app_id'] = trim($param['app_id']);
        }
        if(!empty($param['key'])){
            $extend_json['key'] = trim($param['key']);
        }
        if(!empty($param['notify_url'])){
            $extend_json['notify_url'] = trim($param['notify_url']);
        }
        if(!empty($param['partner_notify_url'])){
            $extend_json['partner_notify_url'] = trim($param['partner_notify_url']);
        }
        if(!empty($param['refund_notify_url'])){
            $extend_json['refund_notify_url'] = trim($param['refund_notify_url']);
        }
        if(!empty($param['private_key'])){
            $extend_json['private_key'] = trim($param['private_key']);
        }
        if(!empty($param['public_key'])){
            $extend_json['public_key'] = trim($param['public_key']);
        }
        if(!empty($param['app_secret'])){
            $extend_json['app_secret'] = trim($param['app_secret']);
        }
        if(!empty($param['mch_id'])){
            $extend_json['mch_id'] = trim($param['mch_id']);
        }

        if(!empty($extend_json)){
            $data['extend_json'] = json_encode($extend_json);
        }

        //图片上传(单图)
        if (!empty($request->file('file'))) {
            $file = $request->file('file');
            $path = "/pay/img/";
            $url = ImageUtil::i()->upload($file, $path);
        }

        if (!empty($url)) {
            $data['icon'] = $url;
        }

        if (!empty($param['is_show'])) {
            if ($param['is_show'] == "on") {
                $data['is_show'] = 1;
            } else {
                $data['is_show'] = 0;
            }
        }else{
            $data['is_show'] = 0;
        }

        if(empty($param['id'])){
            $data['add_time'] = date("Y-m-d H:i:s",time());
            $id = SysPayModel::addSysPay($data);
            if($id){
                return Util::returnJson(self::CODE_SUCCESS,null,"修改成功");
            }else{
                return Util::returnJson(self::CODE_ERROR,null,"修改失败");
            }
        }else{
            $flag = SysPayModel::updateSysPayWhere([['id','=',$param['id']]],$data);
            if($flag){
                return Util::returnJson(self::CODE_SUCCESS,null,"修改成功");
            }else{
                return Util::returnJson(self::CODE_ERROR,null,"修改失败");
            }
        }
    }

    /**
     * 修改状态
     * @param Request $request
     * @return array
     */
    public function editPayType(Request $request)
    {
        $param = $request->post();
        if (!empty($param['is_show'])) {
            if($param['is_show'] == "on"){
                $save['is_show'] = 1;
            }else{
                $save['is_show'] = 0;
            }
        }

        $flag = SysPayModel::updateSysPayWhere([['id', '=', $param['id']]], $save);
        if ($flag) {
            return Util::returnJson(self::CODE_SUCCESS, null, "修改成功");
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "修改失败1");
        }
    }

}